package com.imay.live.activity.family.controller;

import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.R;
import com.imay.live.activity.discovery.FollowControl;
import com.imay.live.activity.family.FamilyParams;
import com.imay.live.activity.family.FamilyPersonActivity;
import com.imay.live.adapter.family.FamilyAcceptAdapter;
import com.imay.live.adapter.family.FamilyUserAdapter;
import com.imay.live.adapter.family.FamilyUserLevelAdapter;
import com.imay.live.helper.DialogFragmentHelper;
import com.imay.live.helper.EventHelper;
import com.imay.live.helper.RepostHelper;
import com.imay.live.model.FamilyUserInfo;
import com.imay.live.model.FamilyUserModel;
import com.imay.live.model.MenuItemModel;
import com.imay.live.model.UserExpsBean;
import com.imay.live.model.UserModel;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.RecyclerViewUtil;
import com.imay.live.utils.ViewUtil;
import com.imay.live.view.dialog.ChoiseDialogFragment;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public class FamilyPersonController extends BaseFamilyController {

    private RecyclerView rl_family_person;
    FamilyUserAdapter mAdapter;

    public static final int MENU_ACTION_FAMILY_USER_FOCUS = 301; //关注
    public static final int MENU_ACTION_FAMILY_USER_REPOST = 302; //拉黑举报
    public static final int MENU_ACTION_FAMILY_USER_NO_FOCUS = 303; // 取消关注
    public static final int MENU_ACTION_FAMILY_USER_OUT = 304; // 踢出家族
    public static final int MENU_ACTION_FAMILY_USER_JOB_OUT = 305; //革除职务
    public static final int MENU_ACTION_FAMILY_USER_JOB_IN = 306; // 任命长老
    public static final int MENU_ACTION_FAMILY_USER_CHANGE_ELDER = 307; //转移会长

    private MenuItemModel FOCUS_MENU = new MenuItemModel(MENU_ACTION_FAMILY_USER_FOCUS, R.string.str_family_menu_focus);
    private MenuItemModel REPOST_MENU = new MenuItemModel(MENU_ACTION_FAMILY_USER_REPOST, R.string.str_family_menu_repost);
    private MenuItemModel NOFOCUS_MENU = new MenuItemModel(MENU_ACTION_FAMILY_USER_NO_FOCUS, R.string.str_family_menu_no_focus);
    private MenuItemModel OUT_MENU = new MenuItemModel(MENU_ACTION_FAMILY_USER_OUT, R.string.str_family_menu_clearout);
    private MenuItemModel JOB_OUT_MENU = new MenuItemModel(MENU_ACTION_FAMILY_USER_JOB_OUT, R.string.str_family_menu_jobout);
    private MenuItemModel JOB_IN_MENU = new MenuItemModel(MENU_ACTION_FAMILY_USER_JOB_IN, R.string.str_family_menu_jobin);
    private MenuItemModel CHANGE_ELDER_MENU = new MenuItemModel(MENU_ACTION_FAMILY_USER_CHANGE_ELDER, R.string.str_family_menu_changeold);

    @Override
    protected void initView() {
        rl_family_person = ViewUtil.findView(activity, R.id.rl_family_person);
        RecyclerViewUtil.setLinearLayoutManager(activity, rl_family_person);
    }

    public void initAdapter(int status) {
        mAdapter = new FamilyUserAdapter(status);
        rl_family_person.setAdapter(mAdapter);
    }

    /**
     * 刷新数据
     *
     * @param userData
     */
    public void setUserData(FamilyUserModel userData) {
        if (userData == null || userData.getFamilyUsers() == null) {
            return;
        }

        List<MultiItemEntity> usersAdmin5 = new ArrayList<>();
        List<MultiItemEntity> usersAdmin1 = new ArrayList<>();
        List<MultiItemEntity> usersAdmin0 = new ArrayList<>();
        List<FamilyUserModel.FamilyUsersBean> familyUsers = userData.getFamilyUsers();

        for (FamilyUserModel.FamilyUsersBean user : familyUsers) {
            if (user.getAdmin() == FamilyParams.ADMIN_BIG) {
                usersAdmin5.add(user);
            } else if (user.getAdmin() == FamilyParams.ADMIN_MID) {
                usersAdmin1.add(user);
            } else if (user.getAdmin() == FamilyParams.ADMIN_SMALL) {
                usersAdmin0.add(user);
            }
        }

        mAdapter.setAdminCount(usersAdmin5.size(), usersAdmin1.size(), usersAdmin0.size());

        List<MultiItemEntity> users = new ArrayList<>();
        if (!usersAdmin5.isEmpty()) {
            users.add(new MultiItemEntity() {
                @Override
                public int getItemType() {
                    return FamilyUserAdapter.VIEWTYPE_LINEAR_CONTENT_TITLE;
                }
            });
            users.addAll(usersAdmin5);
        }

        if (!usersAdmin1.isEmpty()) {
            users.add(new MultiItemEntity() {
                @Override
                public int getItemType() {
                    return FamilyUserAdapter.VIEWTYPE_LINEAR_CONTENT_TITLE;
                }
            });
            users.addAll(usersAdmin1);
        }

        if (!usersAdmin0.isEmpty()) {
            users.add(new MultiItemEntity() {
                @Override
                public int getItemType() {
                    return FamilyUserAdapter.VIEWTYPE_LINEAR_CONTENT_TITLE;
                }
            });
            users.addAll(usersAdmin0);
        }

        mAdapter.setNewData(users);
    }

    /**
     * 加载数据
     *
     * @param data
     */
    public void addUserData(FamilyUserModel data) {
        List<MultiItemEntity> familyUsers = new ArrayList<>();
        familyUsers.addAll(mAdapter.getData());
        familyUsers.addAll(data.getFamilyUsers());

        List<MultiItemEntity> usersAdmin5 = new ArrayList<>();
        List<MultiItemEntity> usersAdmin1 = new ArrayList<>();
        List<MultiItemEntity> usersAdmin0 = new ArrayList<>();

        for (MultiItemEntity item : familyUsers) {
            if (item instanceof FamilyUserModel.FamilyUsersBean) {
                FamilyUserModel.FamilyUsersBean user = (FamilyUserModel.FamilyUsersBean) item;
                if (user.getAdmin() == FamilyParams.ADMIN_BIG) {
                    usersAdmin5.add(user);
                } else if (user.getAdmin() == FamilyParams.ADMIN_MID) {
                    usersAdmin1.add(user);
                } else if (user.getAdmin() == FamilyParams.ADMIN_SMALL) {
                    usersAdmin0.add(user);
                }
            }
        }

        mAdapter.setAdminCount(usersAdmin5.size(), usersAdmin1.size(), usersAdmin0.size());

        List<MultiItemEntity> users = new ArrayList<>();
        if (!usersAdmin5.isEmpty()) {
            users.add(new MultiItemEntity() {
                @Override
                public int getItemType() {
                    return FamilyUserAdapter.VIEWTYPE_LINEAR_CONTENT_TITLE;
                }
            });
            users.addAll(usersAdmin5);
        }

        if (!usersAdmin1.isEmpty()) {
            users.add(new MultiItemEntity() {
                @Override
                public int getItemType() {
                    return FamilyUserAdapter.VIEWTYPE_LINEAR_CONTENT_TITLE;
                }
            });
            users.addAll(usersAdmin1);
        }

        if (!usersAdmin0.isEmpty()) {
            users.add(new MultiItemEntity() {
                @Override
                public int getItemType() {
                    return FamilyUserAdapter.VIEWTYPE_LINEAR_CONTENT_TITLE;
                }
            });
            users.addAll(usersAdmin0);
        }

        mAdapter.setNewData(users);
    }

    public void setFamilyNick(String familyNick) {
        mAdapter.setFamilyNick(familyNick);
    }

    public void setItemClick(BaseQuickAdapter.OnItemChildClickListener itemClick) {
        mAdapter.setOnItemChildClickListener(itemClick);
    }

    /**
     * 用户管理
     *
     * @param admin
     */
    public void initMenu(FamilyUserInfo.FamilyUserBean admin, FamilyUserModel.FamilyUsersBean operAdmin, ChoiseDialogFragment.OnItemClickListener itemClickListener) {
        if (admin == null || operAdmin == null) {
            return;
        }
        if (admin.getUid() == operAdmin.getUid()) {
            return;
        }
        List<MenuItemModel> menu = new ArrayList<>();
        menu.add(REPOST_MENU);
        if (admin.getAdmin() > operAdmin.getAdmin()) {
            switch (admin.getAdmin()) {
                case FamilyParams.ADMIN_BIG:
                    if (operAdmin.getAdmin() == FamilyParams.ADMIN_MID) {
                        menu.add(JOB_OUT_MENU);
                        menu.add(CHANGE_ELDER_MENU);
                    } else {
                        menu.add(JOB_IN_MENU);
                        menu.add(OUT_MENU);
                        menu.add(CHANGE_ELDER_MENU);
                    }
                    break;
                case FamilyParams.ADMIN_MID:
                    menu.add(OUT_MENU);
                    break;
            }
        }
        if (operAdmin.isMyFollow()) { // 已经关注的
            //menu.add(NOFOCUS_MENU);
        } else { // 没有关注的
            menu.add(FOCUS_MENU);
        }
        if (!menu.isEmpty()) {
            ChoiseDialogFragment.show(((AppCompatActivity) activity).getSupportFragmentManager(), menu, this.hashCode(), itemClickListener);
        }
    }


    /**
     * 踢出家族
     *
     * @param targetUid
     */
    public String clearOut(int targetUid) {
        return userManager(targetUid, 1);
    }

    /**
     * 长老任命
     *
     * @param targetUid
     */
    public String jobin(int targetUid) {
        return userManager(targetUid, 4);
    }

    /**
     * 卸任职务
     *
     * @param targetUid
     */
    public String jobout(int targetUid) {
        return userManager(targetUid, 2);
    }

    /**
     * 族长转移
     *
     * @param targetUid
     */
    public String changeElder(int targetUid) {
        return userManager(targetUid, 3);
    }

    private String userManager(int targetUid, int type) {
        if (activity instanceof ProtoCallBack) {
            return ProtoRequestHelper.task(ProtoHttpFactory.getFamilyUserManager(targetUid, type)).setTag(activity.hashCode()).request((ProtoCallBack) activity);
        }
        return null;
    }

    /**
     * 关注&取消关注
     *
     * @param control
     * @param usersBean
     */
    public void changeFoucus(FollowControl control, FamilyUserModel.FamilyUsersBean usersBean) {
        UserModel userModel = new UserModel();
        userModel.setNick(usersBean.getNick());
        userModel.setUid(usersBean.getUid());
        userModel.setMyFollow(usersBean.isMyFollow());
        control.followChanged(activity, userModel);

        int position = -1;
        List<MultiItemEntity> items = mAdapter.getData();
        for (MultiItemEntity item : items) {
            position++;
            if (item.getItemType() == FamilyUserAdapter.VIEWTYPE_LINEAR_CONTENT) {
                FamilyUserModel.FamilyUsersBean userBean = (FamilyUserModel.FamilyUsersBean) item;
                if (userBean.getUid() == usersBean.getUid()) {
                    userBean.setMyFollow(!userBean.isMyFollow());
                    break;
                }
            }
        }
        mAdapter.notifyItemChanged(position);
    }

    /**
     * 举报用户
     *
     * @param fragmentmanager
     * @param nick
     * @param uid
     */
    public void report(final FragmentManager fragmentmanager, String nick, final int uid) {
        RepostHelper.getInstance().reportUser(fragmentmanager, activity, uid, false);
    }

    public void initRresh(RefreshListenerAdapter reshListener) {
        initRreshView(R.id.refresh_view);
        initRreshHead();
        initButtom(true);
        if (refresh_view != null) {
            refresh_view.setOnRefreshListener(reshListener);
        }
    }
}
